/**
 * @author mithun
 */

//var console = loadFirebugConsole();

Event.observe(window, 'load', function(event) {
		
	initScheduling();

});

var WeekDays = function(currentDayNo) {
	
	this.getNextDay = function(dayNo) {
		if (dayNo == 7) {
			return 1;
		} else {
			return dayNo + 1;
		}
	}
	this.getPrevDay = function(dayNo) {
		if (dayNo == 1) {
			return 7;
		} else {
			return dayNo - 1;
		}
	}
	this.dayNo   = parseInt(currentDayNo, 10);
//	this.days    = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
	this.days    = new Array();
	this.days[1] = 'Sunday';
	this.days[2] = 'Monday';
	this.days[3] = 'Tuesday';
	this.days[4] = 'Wednesday';
	this.days[5] = 'Thursday';
	this.days[6] = 'Friday';
	this.days[7] = 'Saturday';
	this.day     = this.days[this.dayNo]; 
	this.nextDay = this.days[this.getNextDay(this.dayNo)]; 
	this.prevDay = this.days[this.getPrevDay(this.dayNo)];
	 
}

var NodeDetails = function(id) {
	
	this.getPrevNodeId = function(nodeId){
		var prevNodeId = null; 
		var nodeList   = document.getElementsByClassName('NodesList');
		for (var i = 1; i < nodeList.length; i++) {
			if (nodeList[i].value == nodeId) {
				prevNodeId = nodeList[i-1].value;
				break; 
			}
		}
		return prevNodeId;
	}
	this.getNodeArrivalDay = function(nodeId, prevNodeId, prevNodeDepartureDay){
		if (prevNodeId == null || prevNodeDepartureDay == null) {
			// Start Node
			var w = new WeekDays($F('RouteScheduleRouteScheduleStartDay'));
			return w.day; 			 
		}
		// Obtain Date object curresponds to previous node time of day
		var d1 = new Date();
		d1.clearTime();
		d1.addHours($F('TimeScheduleTimeScheduleDepartureTime'+prevNodeId+'Hour'));
		d1.addMinutes($F('TimeScheduleTimeScheduleDepartureTime'+prevNodeId+'Min'));
		d1.addSeconds($F('TimeScheduleTimeScheduleDepartureTime'+prevNodeId+'Sec'));
		// Obtain Date object curresponds to current node time of day
		var d2 = new Date();
		d2.clearTime();
		d2.addHours($F('TimeScheduleTimeScheduleArrivalTime'+nodeId+'Hour'));
		d2.addMinutes($F('TimeScheduleTimeScheduleArrivalTime'+nodeId+'Min'));
		d2.addSeconds($F('TimeScheduleTimeScheduleArrivalTime'+nodeId+'Sec'));
		// Compares this instance to a Date object and return an number indication of their relative values. 
		// 1 = this is greater than date, -1 = this is less than date, 0 = values are equal.
		if (d2.compareTo(d1) >= 0) { 
			return prevNodeDepartureDay;
		} else {
			// Get Next day of week
			var w = new WeekDays(Date.getDayNumberFromName(prevNodeDepartureDay)+1);
			return w.nextDay; 			 
		}
		
	}
	this.getNodeDepartureDay = function(nodeId, nodeArrivalDay) {
		/*
		if (prevNodeId == null || prevNodeDay == null) {
			// Start Node
			var w = new WeekDays($('RouteScheduleRouteScheduleStartDay').getValue());
			return w.day; 			 
		}
		*/
		if (nodeId == null || nodeArrivalDay == null) {
			// Start Node
			var w = new WeekDays($F('RouteScheduleRouteScheduleStartDay'));
			return w.day; 			 
		}
		// Obtain Date object curresponds to previous node time of day
		var d1 = new Date();
		d1.clearTime();
		d1.addHours($F('TimeScheduleTimeScheduleArrivalTime'+nodeId+'Hour'));
		d1.addMinutes($F('TimeScheduleTimeScheduleArrivalTime'+nodeId+'Min'));
		d1.addSeconds($F('TimeScheduleTimeScheduleArrivalTime'+nodeId+'Sec'));
		// Obtain Date object curresponds to current node time of day
		var d2 = new Date();
		d2.clearTime();
		d2.addHours($F('TimeScheduleTimeScheduleDepartureTime'+nodeId+'Hour'));
		d2.addMinutes($F('TimeScheduleTimeScheduleDepartureTime'+nodeId+'Min'));
		d2.addSeconds($F('TimeScheduleTimeScheduleDepartureTime'+nodeId+'Sec'));
		// Compares this instance to a Date object and return an number indication of their relative values. 
		// 1 = this is greater than date, -1 = this is less than date, 0 = values are equal.
		if (d2.compareTo(d1) >= 0) { 
			return nodeArrivalDay;
		} else {
			// Get Next day of week
			var w = new WeekDays(Date.getDayNumberFromName(nodeArrivalDay)+1);
			return w.nextDay; 			 
		}
		
	}
	this.id         = id;
	this.prevNodeId = this.getPrevNodeId(this.id);
	if (this.prevNodeId == null) {
		this.prevNodeDepartureTime = null;
		this.prevNodeDepartureDay  = null;
	} else {
		this.prevNodeDepartureTime = $F('TimeScheduleTimeScheduleDepartureTime'+this.prevNodeId+'Hour')+":"+$F('TimeScheduleTimeScheduleDepartureTime'+this.prevNodeId+'Min')+":"+$F('TimeScheduleTimeScheduleDepartureTime'+this.prevNodeId+'Sec');
		this.prevNodeDepartureDay  = $F('TimeScheduleTimeScheduleDepartureDayLabel'+this.prevNodeId);
	}
	this.arrivalTime     = $F('TimeScheduleTimeScheduleArrivalTime'+this.id+'Hour')  +  ":" + $F('TimeScheduleTimeScheduleArrivalTime'   + this.id+'Min') + ":" + $F('TimeScheduleTimeScheduleArrivalTime'  + this.id+'Sec');
	this.departureTime   = $F('TimeScheduleTimeScheduleDepartureTime'+this.id+'Hour') + ":" + $F('TimeScheduleTimeScheduleDepartureTime' + this.id+'Min') + ":" + $F('TimeScheduleTimeScheduleDepartureTime'+ this.id+'Sec');
	this.arrivalDay      = this.getNodeArrivalDay(this.id, this.prevNodeId, this.prevNodeDepartureDay);
	this.departureDay    = this.getNodeDepartureDay(this.id, this.arrivalDay);
	this.arrivalDayNo    = (this.arrivalDay) ? Date.getDayNumberFromName(this.arrivalDay)+1 : null;
	this.departureDayNo  = (this.departureDay) ? Date.getDayNumberFromName(this.departureDay)+1 : null;
	
}

var initScheduling = function() {
	
	var scheduleDayElements = document.getElementsByClassName('autoSchedule');
	var startDayNo = $F('RouteScheduleRouteScheduleStartDay'); 
	for (var i=1; i<scheduleDayElements.length; i=i+2) {
		el = scheduleDayElements[i];
		var tempId = el.id;
		if(tempId.match("TimeScheduleTimeScheduleArrivalDayLabel")) {
			var nodeId = parseInt(tempId.replace("TimeScheduleTimeScheduleArrivalDayLabel", ""), 10);
		} else if(tempId.match("TimeScheduleTimeScheduleDepartureDayLabel")) {
			var nodeId = parseInt(tempId.replace("TimeScheduleTimeScheduleDepartureDayLabel", ""), 10);
		}
		/*
		$('TimeScheduleTimeScheduleArrivalTime'+nodeId+'Hour').enable();
		$('TimeScheduleTimeScheduleArrivalTime'+nodeId+'Min').enable();
		$('TimeScheduleTimeScheduleArrivalTime'+nodeId+'Sec').enable();
		$('TimeScheduleTimeScheduleDepartureTime'+nodeId+'Hour').enable();
		$('TimeScheduleTimeScheduleDepartureTime'+nodeId+'Min').enable();
		$('TimeScheduleTimeScheduleDepartureTime'+nodeId+'Sec').enable();
		*/
		$('scheduleDepartureTimeWarn-'+nodeId).innerHTML = "";
		$('scheduleArrivalTimeWarn-'+nodeId).innerHTML = "";
		if ($F('TimeScheduleTimeScheduleArrivalTime'+nodeId+'Hour') && $F('TimeScheduleTimeScheduleArrivalTime'+nodeId+'Min') && $F('TimeScheduleTimeScheduleArrivalTime'+nodeId+'Sec')) {
			//$('scheduleArrivalTimeWarn-'+nodeId).innerHTML  = "Scheduled.";
			//$('scheduleDepartureTimeWarn-'+nodeId).innerHTML  = "Scheduled.";
		} else {
			$('TimeScheduleTimeScheduleArrivalDay'+nodeId).value = 0;
			el.value = "Not scheduled";
		}
			
	}
	
}

var adjustStartDay = function(el) {
	
	var scheduleDayElements = document.getElementsByClassName('autoSchedule');
	var startDayNo          = $F('RouteScheduleRouteScheduleStartDay'); 
	switch (startDayNo) {
		case "" :
			break;
		case '-1' :
			for (var i=0; i<scheduleDayElements.length; i++) {
				el = scheduleDayElements[i];
				if (el.type == "hidden") {
					el.value = -1;
				}
				if (el.type == "text") {
					el.value = "Daily";
				}
			}
			break;
		case '1' :
		case '2' :
		case '3' :
		case '4' :
		case '5' :
		case '6' :
		case '7' :
			var dayDifference = startDayNo - parseInt(scheduleDayElements[0].value, 10);
			scheduleDayElements[0].value = startDayNo;
			scheduleDayElements[1].value = new WeekDays(startDayNo).day;
			var newDay = null;
			for (var i = 2; i < scheduleDayElements.length; i++) {
				el = scheduleDayElements[i];
				if (el.type == "hidden" && el.value != '') {
					newDay = parseInt(el.value, 10) + dayDifference;
					el.value = newDay;
				}
				if (el.type == "text" && el.value != '') {
					var tempId = el.id;
					if(tempId.match("TimeScheduleTimeScheduleArrivalDayLabel")) {
						var nodeId = parseInt(tempId.replace("TimeScheduleTimeScheduleArrivalDayLabel", ""), 10);
					} else if(tempId.match("TimeScheduleTimeScheduleDepartureDayLabel")) {
						var nodeId = parseInt(tempId.replace("TimeScheduleTimeScheduleDepartureDayLabel", ""), 10);
					}
					if ($F('TimeScheduleTimeScheduleArrivalTime'+nodeId+'Hour') && $F('TimeScheduleTimeScheduleArrivalTime'+nodeId+'Min')) {
						var nodeDetails = new NodeDetails(nodeId);
						$('TimeScheduleTimeScheduleArrivalDay'+nodeId).value        = nodeDetails.arrivalDayNo; 
						$('TimeScheduleTimeScheduleArrivalDayLabel'+nodeId).value   = nodeDetails.arrivalDay; 
						$('TimeScheduleTimeScheduleDepartureDay'+nodeId).value      = nodeDetails.departureDayNo; 
						$('TimeScheduleTimeScheduleDepartureDayLabel'+nodeId).value = nodeDetails.departureDay; 
					} else {
						$('TimeScheduleTimeScheduleArrivalDay'+nodeId).value      = 0; 
						$('TimeScheduleTimeScheduleArrivalDayLabel'+nodeId).value = "Not scheduled"; 
					}
				}
			}
			break;
		default :
			break;
	}
		
}

var autoScheduleTiming = function(el, nodeId) {
	
	$('routeScheduleValidationWarn').innerHTML       = "";
	$('scheduleDepartureTimeWarn-'+nodeId).innerHTML = "";
	$('scheduleArrivalTimeWarn-'+nodeId).innerHTML   = "";
	
	if (el.id.match("TimeScheduleTimeScheduleArrivalTime")) {
		if (($('TimeScheduleTimeScheduleArrivalTime' + nodeId + 'Hour').selectedIndex == 0) || ($('TimeScheduleTimeScheduleArrivalTime' + nodeId + 'Min').selectedIndex == 0) || ($('TimeScheduleTimeScheduleArrivalTime' + nodeId + 'Sec').selectedIndex == 0)) {
			$('TimeScheduleTimeScheduleArrivalDay'+nodeId).value      = 0; 
			$('TimeScheduleTimeScheduleArrivalDayLabel'+nodeId).value = "Not scheduled"; 
		}
	}		
	if (el.id.match("TimeScheduleTimeScheduleDepartureTime")) {
		if (($('TimeScheduleTimeScheduleDepartureTime' + nodeId + 'Hour').selectedIndex == 0) || ($('TimeScheduleTimeScheduleDepartureTime' + nodeId + 'Min').selectedIndex == 0) || ($('TimeScheduleTimeScheduleDepartureTime' + nodeId + 'Sec').selectedIndex == 0)) {
			$('TimeScheduleTimeScheduleDepartureDay' + nodeId).value = 0;
			$('TimeScheduleTimeScheduleDepartureDayLabel' + nodeId).value = "Not scheduled";
		} 
	}
	
	if ($F('RouteScheduleRouteScheduleStartDay')=='') {
		el.value = null;
		alert('Please select a Start day first');
		return;
	} else if ($F('RouteScheduleRouteScheduleStartDay') =='-1') {
		var nodeDetails = new NodeDetails(nodeId);
		
		
		if (nodeDetails.prevNodeId != null) {
			if($('TimeScheduleTimeScheduleDepartureTime'+nodeId+'Min').selectedIndex == 0) {
				// wait; do nothig till minutes field is selected
				return;
			}
			if (nodeDetails.prevNodeDepartureDay != "Daily") {
				$('TimeScheduleTimeScheduleArrivalTime'+nodeId+'Hour').selectedIndex    = 0; 
				$('TimeScheduleTimeScheduleArrivalTime'+nodeId+'Min').selectedIndex     = 0; 
				$('TimeScheduleTimeScheduleArrivalTime'+nodeId+'Sec').selectedIndex     = 0; 
				$('TimeScheduleTimeScheduleDepartureTime'+nodeId+'Hour').selectedIndex  = 0; 
				$('TimeScheduleTimeScheduleDepartureTime'+nodeId+'Min').selectedIndex   = 0; 
				$('TimeScheduleTimeScheduleDepartureTime'+nodeId+'Sec').selectedIndex   = 0; 
				$('TimeScheduleTimeScheduleArrivalDay'+nodeId).value                    = 0; 
				$('TimeScheduleTimeScheduleArrivalDayLabel'+nodeId).value               = "Not scheduled"; 
				$('TimeScheduleTimeScheduleDepartureDay' + nodeId).value                = 0;
				$('TimeScheduleTimeScheduleDepartureDayLabel' + nodeId).value           = "Not scheduled";
				$('scheduleArrivalTimeWarn-'+nodeId).innerHTML  = "Schedule previous node first.";
				$('scheduleDepartureTimeWarn-'+nodeId).innerHTML  = "Schedule previous node first.";
				//alert("Please schedule previous node first.")
				return;
			}
		}
		if (el.id.match("TimeScheduleTimeScheduleDepartureTime")) {
			
			if (($('TimeScheduleTimeScheduleArrivalTime'+nodeId+'Hour').selectedIndex == 0) || ($('TimeScheduleTimeScheduleArrivalTime'+nodeId+'Min').selectedIndex == 0) || ($('TimeScheduleTimeScheduleArrivalTime'+nodeId+'Sec').selectedIndex == 0)) {
				//alert("Please schedule Node Arrival time first.");
				$('scheduleDepartureTimeWarn-'+nodeId).innerHTML  = "Please schedule Arrival time first.";
				$('TimeScheduleTimeScheduleDepartureTime'+nodeId+'Hour').selectedIndex  = 0; 
				$('TimeScheduleTimeScheduleDepartureTime'+nodeId+'Min').selectedIndex   = 0; 
				$('TimeScheduleTimeScheduleDepartureTime'+nodeId+'Sec').selectedIndex   = 0; 
				$('TimeScheduleTimeScheduleDepartureDay' + nodeId).value                = 0;
				$('TimeScheduleTimeScheduleDepartureDayLabel' + nodeId).value           = "Not scheduled";
				return;	 
			}
			if($('TimeScheduleTimeScheduleDepartureTime'+nodeId+'Min').selectedIndex ==0 ) {
				// wait
				return;
			}
			var d1 = new Date();
			d1.clearTime();
			d1.addHours($F('TimeScheduleTimeScheduleArrivalTime'+nodeId+'Hour'));
			d1.addMinutes($F('TimeScheduleTimeScheduleArrivalTime'+nodeId+'Min'));
			d1.addSeconds($F('TimeScheduleTimeScheduleArrivalTime'+nodeId+'Sec'));
			// Obtain Date object curresponds to current node time of day
			var d2 = new Date();
			d2.clearTime();
			d2.addHours($F('TimeScheduleTimeScheduleDepartureTime'+nodeId+'Hour'));
			d2.addMinutes($F('TimeScheduleTimeScheduleDepartureTime'+nodeId+'Min'));
			d2.addSeconds($F('TimeScheduleTimeScheduleDepartureTime'+nodeId+'Sec'));
			// Compares this instance to a Date object and return an number indication of their relative values. 
			// 1 = this is greater than date, -1 = this is less than date, 0 = values are equal.
			if (d1.compareTo(d2) > 0) { 
				$('TimeScheduleTimeScheduleDepartureTime'+nodeId+'Hour').selectedIndex  = 0; 
				$('TimeScheduleTimeScheduleDepartureTime'+nodeId+'Min').selectedIndex   = 0; 
				$('TimeScheduleTimeScheduleDepartureTime'+nodeId+'Sec').selectedIndex   = 0;
				$('TimeScheduleTimeScheduleArrivalDay'+nodeId).value                    = 0; 
				$('TimeScheduleTimeScheduleArrivalDayLabel'+nodeId).value               = "Not scheduled"; 
				//alert("Departure time should be greater than Arrival time.")
				$('scheduleDepartureTimeWarn-'+nodeId).innerHTML  = "Departure time should be greater than Arrival time.";
				return;	 
			}
		}
		if (nodeDetails.prevNodeId != null) {
			if (nodeDetails.prevNodeDepartureDay != 'Daily') {
				$('TimeScheduleTimeScheduleArrivalTime'+nodeId+'Hour').selectedIndex    = 0; 
				$('TimeScheduleTimeScheduleArrivalTime'+nodeId+'Min').selectedIndex     = 0; 
				$('TimeScheduleTimeScheduleArrivalTime'+nodeId+'Sec').selectedIndex     = 0; 
				$('TimeScheduleTimeScheduleDepartureTime'+nodeId+'Hour').selectedIndex  = 0; 
				$('TimeScheduleTimeScheduleDepartureTime'+nodeId+'Min').selectedIndex   = 0; 
				$('TimeScheduleTimeScheduleDepartureTime'+nodeId+'Sec').selectedIndex   = 0; 
				$('TimeScheduleTimeScheduleArrivalDay'+nodeId).value                    = 0; 
				$('TimeScheduleTimeScheduleArrivalDayLabel'+nodeId).value               = "Not scheduled"; 
				$('TimeScheduleTimeScheduleDepartureDay' + nodeId).value                = 0;
				$('TimeScheduleTimeScheduleDepartureDayLabel' + nodeId).value           = "Not scheduled";
				//alert("Please schedule previous node first.")
				$('scheduleArrivalTimeWarn-'+nodeId).innerHTML  = "Schedule previous node first.";
				$('scheduleDepartureTimeWarn-'+nodeId).innerHTML  = "Schedule previous node first.";
				return;
			}
			// Obtain Date object curresponds to previous node time of day
			var d1 = new Date();
			d1.clearTime();
			d1.addHours($F('TimeScheduleTimeScheduleArrivalTime'+nodeId+'Hour'));
			d1.addMinutes($F('TimeScheduleTimeScheduleArrivalTime'+nodeId+'Min'));
			d1.addSeconds($F('TimeScheduleTimeScheduleArrivalTime'+nodeId+'Sec'));
			// Obtain Date object curresponds to current node time of day
			var d2 = new Date();
			d2.clearTime();
			d2.addHours($F('TimeScheduleTimeScheduleDepartureTime'+nodeDetails.prevNodeId+'Hour'));
			d2.addMinutes($F('TimeScheduleTimeScheduleDepartureTime'+nodeDetails.prevNodeId+'Min'));
			d2.addSeconds($F('TimeScheduleTimeScheduleDepartureTime'+nodeDetails.prevNodeId+'Sec'));
			// Compares this instance to a Date object and return an number indication of their relative values. 
			// 1 = this is greater than date, -1 = this is less than date, 0 = values are equal.
			if (d2.compareTo(d1) >= 0) { 
				$('TimeScheduleTimeScheduleArrivalTime'+nodeId+'Hour').selectedIndex = 0; 
				$('TimeScheduleTimeScheduleArrivalTime'+nodeId+'Min').selectedIndex  = 0; 
				$('TimeScheduleTimeScheduleArrivalTime'+nodeId+'Sec').selectedIndex  = 0;
				$('TimeScheduleTimeScheduleArrivalDay'+nodeId).value                 = 0; 
				$('TimeScheduleTimeScheduleArrivalDayLabel'+nodeId).value            = "Not scheduled"; 
				//alert("This time should be greater than previous node Departure time.");
				$('scheduleArrivalTimeWarn-'+nodeId).innerHTML  = "Should be greater than previous Departure time.";
				return;	 
			} 
		} 
		if ($F('TimeScheduleTimeScheduleArrivalTime'+nodeId+'Hour') && $F('TimeScheduleTimeScheduleArrivalTime'+nodeId+'Min') && $F('TimeScheduleTimeScheduleArrivalTime'+nodeId+'Sec')) {
			$('TimeScheduleTimeScheduleArrivalDay'+nodeId).value      = -1; 
			$('TimeScheduleTimeScheduleArrivalDayLabel'+nodeId).value = "Daily"; 
		} else {
			$('TimeScheduleTimeScheduleArrivalDay'+nodeId).value      = 0; 
			$('TimeScheduleTimeScheduleArrivalDayLabel'+nodeId).value = "Not scheduled"; 
		}
		if ($F('TimeScheduleTimeScheduleDepartureTime'+nodeId+'Hour') && $F('TimeScheduleTimeScheduleDepartureTime'+nodeId+'Min') && $F('TimeScheduleTimeScheduleDepartureTime'+nodeId+'Sec')) {
			$('TimeScheduleTimeScheduleDepartureDay'+nodeId).value      = -1; 
			$('TimeScheduleTimeScheduleDepartureDayLabel'+nodeId).value = "Daily"; 
		} else {
			$('TimeScheduleTimeScheduleArrivalDay'+nodeId).value      = 0; 
			$('TimeScheduleTimeScheduleArrivalDayLabel'+nodeId).value = "Not scheduled"; 
		}
		return;
	}
	var nodeDetails = new NodeDetails(nodeId);
	if (nodeDetails.prevNodeId) {
		if ($F('TimeScheduleTimeScheduleArrivalDay'+nodeDetails.prevNodeId) == 0) {
			$('TimeScheduleTimeScheduleArrivalTime'+nodeId+'Hour').selectedIndex   = 0; 
			$('TimeScheduleTimeScheduleArrivalTime'+nodeId+'Min').selectedIndex    = 0;
			$('TimeScheduleTimeScheduleArrivalTime'+nodeId+'Sec').selectedIndex    = 0;
			$('TimeScheduleTimeScheduleDepartureTime'+nodeId+'Hour').selectedIndex = 0; 
			$('TimeScheduleTimeScheduleDepartureTime'+nodeId+'Min').selectedIndex  = 0;
			$('TimeScheduleTimeScheduleDepartureTime'+nodeId+'Sec').selectedIndex  = 0;
			$('TimeScheduleTimeScheduleArrivalDay'+nodeId).value                   = 0; 
			$('TimeScheduleTimeScheduleDepartureDay'+nodeId).value                 = 0; 
			$('TimeScheduleTimeScheduleArrivalDayLabel'+nodeId).value              = "Not scheduled"; 
			$('TimeScheduleTimeScheduleDepartureDayLabel'+nodeId).value            = "Not scheduled"; 
			//alert('Please schedule previous node first');
			$('scheduleArrivalTimeWarn-'+nodeId).innerHTML  = "Schedule previous node first.";
			$('scheduleDepartureTimeWarn-'+nodeId).innerHTML  = "Schedule previous node first.";
			return;
		} else if ($F('TimeScheduleTimeScheduleDepartureDay'+nodeDetails.prevNodeId) == 0) {
			$('TimeScheduleTimeScheduleArrivalTime'+nodeId+'Hour').selectedIndex   = 0; 
			$('TimeScheduleTimeScheduleArrivalTime'+nodeId+'Min').selectedIndex    = 0;
			$('TimeScheduleTimeScheduleArrivalTime'+nodeId+'Sec').selectedIndex    = 0;
			$('TimeScheduleTimeScheduleDepartureTime'+nodeId+'Hour').selectedIndex = 0; 
			$('TimeScheduleTimeScheduleDepartureTime'+nodeId+'Min').selectedIndex  = 0;
			$('TimeScheduleTimeScheduleDepartureTime'+nodeId+'Sec').selectedIndex  = 0;
			$('TimeScheduleTimeScheduleArrivalDay'+nodeId).value                   = 0; 
			$('TimeScheduleTimeScheduleDepartureDay'+nodeId).value                 = 0; 
			$('TimeScheduleTimeScheduleArrivalDayLabel'+nodeId).value              = "Not scheduled"; 
			$('TimeScheduleTimeScheduleDepartureDayLabel'+nodeId).value            = "Not scheduled"; 
			//alert('Please schedule previous node first');
			$('scheduleArrivalTimeWarn-'+nodeId).innerHTML  = "Schedule previous node first.";
			$('scheduleDepartureTimeWarn-'+nodeId).innerHTML  = "Schedule previous node first.";
		}
	}

	if ($F('TimeScheduleTimeScheduleArrivalTime'+nodeId+'Hour') && $F('TimeScheduleTimeScheduleArrivalTime'+nodeId+'Min') && $F('TimeScheduleTimeScheduleArrivalTime'+nodeId+'Sec')) {
		$('TimeScheduleTimeScheduleArrivalDay'+nodeId).value      = nodeDetails.arrivalDayNo; 
		$('TimeScheduleTimeScheduleArrivalDayLabel'+nodeId).value = nodeDetails.arrivalDay;
		if (nodeDetails.prevNodeId) {
			/*
			$('TimeScheduleTimeScheduleArrivalTime'+nodeDetails.prevNodeId+'Hour').disable(); 
			$('TimeScheduleTimeScheduleArrivalTime'+nodeDetails.prevNodeId+'Min').disable(); 
			$('TimeScheduleTimeScheduleArrivalTime'+nodeDetails.prevNodeId+'Sec').disable();
			*/ 
		}
	} 
	if ($F('TimeScheduleTimeScheduleDepartureTime'+nodeId+'Hour') && $F('TimeScheduleTimeScheduleDepartureTime'+nodeId+'Min') && $F('TimeScheduleTimeScheduleDepartureTime'+nodeId+'Sec')) {
		$('TimeScheduleTimeScheduleDepartureDay'+nodeId).value      = nodeDetails.departureDayNo; 
		$('TimeScheduleTimeScheduleDepartureDayLabel'+nodeId).value = nodeDetails.departureDay;
		if (nodeDetails.prevNodeId) {
			/*
			$('TimeScheduleTimeScheduleDepartureTime'+nodeDetails.prevNodeId+'Hour').disable(); 
			$('TimeScheduleTimeScheduleDepartureTime'+nodeDetails.prevNodeId+'Min').disable(); 
			$('TimeScheduleTimeScheduleDepartureTime'+nodeDetails.prevNodeId+'Sec').disable();
			*/ 
		}
	}
		disablePreviousScheduleChanges(nodeId);
		unscheduleRemaining(nodeId);
		/*
		$('TimeScheduleTimeScheduleArrivalDay'+nodeId).value        = 0; 
		$('TimeScheduleTimeScheduleDepartureDay'+nodeId).value      = 0; 
		$('TimeScheduleTimeScheduleArrivalDayLabel'+nodeId).value   = "Not scheduled"; 
		$('TimeScheduleTimeScheduleDepartureDayLabel'+nodeId).value = "Not scheduled";
		*/ 
	 
}

var disablePreviousScheduleChanges  = function(nodeId) {
	
	// Do nothing now
	return;
	
	var scheduleDayElements = document.getElementsByClassName('autoSchedule');
	var flag = true;
	for (var i = 0; i < scheduleDayElements.length; i++) {
		if (scheduleDayElements[i].type == "hidden") {
			var tempId = scheduleDayElements[i].id;
			if(tempId.match("TimeScheduleTimeScheduleArrivalDay")) {
				var tempNodeId = parseInt(tempId.replace("TimeScheduleTimeScheduleArrivalDay", ""), 10);
			} else if(tempId.match("TimeScheduleTimeScheduleDepartureDay")) {
				var tempNodeId = parseInt(tempId.replace("TimeScheduleTimeScheduleDepartureDay", ""), 10);
			}
			if (tempNodeId == nodeId) {
				flag = false;
			}
			if (flag) {
				$('TimeScheduleTimeScheduleArrivalTime'+tempNodeId+'Hour').disable();
				$('TimeScheduleTimeScheduleArrivalTime'+tempNodeId+'Min').disable();
				/*	
				 Feature 
				$('TimeScheduleTimeScheduleDepartureTime'+tempNodeId+'Hour').disable();
				$('TimeScheduleTimeScheduleDepartureTime'+tempNodeId+'Min').disable();
				
				if(tempId.match("TimeScheduleTimeScheduleArrivalDayLabel")) {
					$('TimeScheduleTimeScheduleArrivalTime'+tempId+'Hour').disable();
				} else if(tempId.match("TimeScheduleTimeScheduleDepartureDayLabel")) {
					$('TimeScheduleTimeScheduleArrivalTime'+tempId+'Min').disable();
				}
				*/
			}
		}
	}
	
}

var unscheduleRemaining = function(nodeId) {
	
	// Do nothing now
	return;

	var scheduleDayElements = document.getElementsByClassName('autoSchedule');
	var flag = false;
	for (var i = 0; i < scheduleDayElements.length; i++) {
		if (scheduleDayElements[i].type == "hidden") {
			var tempId = scheduleDayElements[i].id;
			if (tempId.match("TimeScheduleTimeScheduleArrivalDay")) {
				tempId = parseInt(tempId.replace("TimeScheduleTimeScheduleArrivalDay", ""), 10);
			} else if(tempId.match("TimeScheduleTimeScheduleDepartureDay")) {
				tempId = parseInt(tempId.replace("TimeScheduleTimeScheduleDepartureDay", ""), 10);
			}
			if (flag && tempId != nodeId) {
				$('TimeScheduleTimeScheduleArrivalDay'+tempId).value      = 0; 
				$('TimeScheduleTimeScheduleArrivalDayLabel'+tempId).value = "Not scheduled"; 
				$('TimeScheduleTimeScheduleArrivalTime'+tempId+'Hour').selectedIndex = 0; 
				$('TimeScheduleTimeScheduleArrivalTime'+tempId+'Min').selectedIndex = 0; 
				$('TimeScheduleTimeScheduleArrivalTime'+tempId+'Sec').selectedIndex = 0; 
			}
			if (tempId == nodeId) {
				flag = true;
			}
		}
	}
		
}

var checkSchedule = function(el) {
	
	var flag = true;
	var scheduleDayElements = document.getElementsByClassName('autoSchedule');
	for (var i = 0; i < scheduleDayElements.length; i++) {
		if (scheduleDayElements[i].type == "hidden") {
			var tempId = scheduleDayElements[i].id;
			if(tempId.match("TimeScheduleTimeScheduleArrivalDay")) {
				tempId = parseInt(tempId.replace("TimeScheduleTimeScheduleArrivalDay", ""));
			} else if(tempId.match("TimeScheduleTimeScheduleDepartureDay")) {
				tempId = parseInt(tempId.replace("TimeScheduleTimeScheduleDepartureDay", ""));
			}
			if (!parseInt($F('TimeScheduleTimeScheduleArrivalDay'+tempId), 10) || !parseInt($F('TimeScheduleTimeScheduleDepartureDay'+tempId), 10)) {
				flag = false;
				$('routeScheduleValidationWarn').innerHTML  = "<strong>Please enter arrival and departure time for all nodes.</strong>";
				break;
			}
			/*
			if($('TimeScheduleTimeScheduleArrivalTime'+tempId+'Hour').selectedIndex  == 0 ||
				$('TimeScheduleTimeScheduleArrivalTime'+tempId+'Min').selectedIndex == 0 ||
				$('TimeScheduleTimeScheduleArrivalTime'+tempId+'Sec').selectedIndex == 0 ||
				$('TimeScheduleTimeScheduleDepartureTime'+tempId+'Hour').selectedIndex == 0 ||
				$('TimeScheduleTimeScheduleDepartureTime'+tempId+'Min').selectedIndex == 0 ||
				$('TimeScheduleTimeScheduleDepartureTime'+tempId+'Sec').selectedIndex == 0 
			) {
				flag = false;
				alert("Please enter arrival and departure time for all nodes.");
				break;
			}
			$('TimeScheduleTimeScheduleArrivalTime'+tempId+'Hour').enable(); 
			$('TimeScheduleTimeScheduleArrivalTime'+tempId+'Min').enable(); 
			$('TimeScheduleTimeScheduleDepartureTime'+tempId+'Hour').enable(); 
			$('TimeScheduleTimeScheduleDepartureTime'+tempId+'Min').enable();
			*/ 
		}
	}
	
	if (flag) {
		submitFormViaJavaScript(el.form);
	}
		
	return flag;
	
}

var resetSchedule = function() {
	
	document.forms[0].reset();
	initScheduling();
	
}
